package thesis.prototype;

import java.util.ArrayList;

import thesis.prototype.objects.Contact;
import thesis.prototype.objects.Meeting;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
//import android.location.LocationListener;
//import android.location.LocationManager;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
//import android.widget.Toast;

/**
 * This activity encompasses 2 screens: Main and Start.
 * (Replaced original MainActivity.java which clumped everything in one)
 * @author Mike
 *
 */
public class HelloActivity extends Activity implements OnClickListener {

	// Constants
	public static final int ORGANIZE_MEMBERS_INTENT = 10;
	public static final int ADD_NEWNUMBER_INTENT = 11;
	public static final int ORGANIZE_DETAILS_INTENT = 20;
	public static final int SHOW_MAP_INTENT = 30;
	public static final int VIEW_MEETINGS_INTENT = 40;
	public static final int MEETING_DETAILS_INTENT = 41;
	//private static final int LOCATION_MIN_TIME = 90000;
	//private static final int LOCATION_MIN_DIST = 50;
	
	// Views
	private View view_main;
	private View view_start;
	// Buttons
	private Button b_main_meet; // from MAIN --> START
	private Button b_main_close; // CLOSE APPLICATION
	private Button b_start_org; // START --> ORGANIZE
	private Button b_start_view; // START --> VIEW
	private Button b_start_back; // START --> MAIN
	
	// Location management
	//public static LocationManager locationManager;
	//public static LocationListener locationListener;
	
	// Data used by other screens
	public static String ownNumber;	
	public static ArrayList<Contact> arr_invited;
	public static String meet_title;
	public static String meet_date;
	public static String meet_time;
	//public static Location location_current;
	public static Meeting viewing_meeting;
	
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		LayoutInflater inflator = 
			(LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
		view_main = inflator.inflate(R.layout.main, null);
		view_start = inflator.inflate(R.layout.start, null);
		
		b_main_meet = (Button) view_main.findViewById(R.id.main_meet);
		b_main_close = (Button) view_main.findViewById(R.id.main_close);
		b_start_org = (Button) view_start.findViewById(R.id.start_organize);
		b_start_view = (Button) view_start.findViewById(R.id.start_viewMeetings);
		b_start_back = (Button) view_start.findViewById(R.id.start_back);
		
		addButtonListeners();
		
		arr_invited = null;
		
		setContentView(view_main);
		
		TelephonyManager teleManager =(TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		ownNumber = teleManager.getLine1Number();
		
		Intent locationService = new Intent();
		locationService.setAction("thesis.prototype.LocationUpdaterService");
		startService(locationService);
	}
	
	public void onDestroy()
	{
		super.onDestroy();
		//Toast t = Toast.makeText(this, "onDestroy", Toast.LENGTH_SHORT);
		//locationManager.removeUpdates(locationListener);
		
	}
	
	private void addButtonListeners()
	{
		if (b_main_meet != null) b_main_meet.setOnClickListener(this);
		if (b_main_close != null) b_main_close.setOnClickListener(this);
		if (b_start_org != null) b_start_org.setOnClickListener(this);
		if (b_start_view != null) b_start_view.setOnClickListener(this);
		if (b_start_back != null) b_start_back.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View view) {
		if(view==b_main_meet)
		{
			setContentView(view_start);
		}
		else if(view==b_main_close)
		{
			setResult(RESULT_OK);
			finish();
		}
		else if(view==b_start_back)
		{
			setContentView(view_main);
		}
		else if(view==b_start_org)
		{
			Intent intent = new Intent(this, thesis.prototype.MembersActivity.class);
			startActivityForResult(intent, ORGANIZE_MEMBERS_INTENT);
		}
		else if(view==b_start_view)
		{
			Intent intent = new Intent(this, thesis.prototype.ViewMeetingsActivity.class);
			startActivityForResult(intent, VIEW_MEETINGS_INTENT);	
		}
	}
	
	@SuppressWarnings("unchecked")
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);
		switch(requestCode)
		{
		case ORGANIZE_MEMBERS_INTENT:
			if(resultCode==RESULT_OK)
			{
				/*arr_invited = (ArrayList<Contact>) data.getExtras().get("thesis.prototype.MembersActivity.invited");
				Intent intent = new Intent(this, thesis.prototype.DetailsActivity.class);
				startActivityForResult(intent, ORGANIZE_DETAILS_INTENT);*/
			}
			else if(resultCode==RESULT_CANCELED)
			{
				// Result canceled: Clean up everything
				arr_invited = null;
				meet_title = null;
				meet_date = null;
				meet_time = null;
				//location_current = null;
			}
			break;
		case VIEW_MEETINGS_INTENT:
			// TODO
			if(resultCode==RESULT_OK)
			{
			}
			else if(resultCode==RESULT_CANCELED)
			{
				viewing_meeting = null;
			}
			break;
		}
	}
}
